﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Net.Sockets;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;

namespace Central_Control.展厅
{
    internal class PCAdmini:INotifyPropertyChanged
    {
        UserControlGallery galleryFA;
        private UdpClient udpClient;
        private ObservableCollection<PcInfo> pCinfos;
        public ObservableCollection<PcInfo> PCinfos
        {
            get { return pCinfos; }

            set { pCinfos = value; OnPropertyChanged(nameof(PCinfos)); }
        }
        public PCAdmini(UserControlGallery gallery) 
        { 
        this.galleryFA = gallery;
        }
        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
        public event PropertyChangedEventHandler? PropertyChanged;


        public void Init(ItemsControl itemsControl)
        {
            udpClient = new UdpClient();
            PCinfos = new ObservableCollection<PcInfo>();
            if(galleryFA.galleryButtonInfo.AddPcInfos!=null&& galleryFA.galleryButtonInfo.AddPcInfos.Count > 0)
            {
                for (int i = 0; i < galleryFA.galleryButtonInfo.AddPcInfos.Count; i++)
                {
                    if (galleryFA.galleryButtonInfo.AddPcInfos[i].isAdd)
                        PCinfos.Add(galleryFA.galleryButtonInfo.AddPcInfos[i]);
                }
            }

            itemsControl.ItemsSource = PCinfos;
        }

        public async Task WakeUpAsync(string ipAddress, string macAddress)
        {
            try
            {
                // 构造唤醒魔术包并发送
                byte[] macBytes = GetMacBytes(macAddress);
                byte[] packet = new byte[17 * 6];

                // 构造魔术包
                for (int i = 0; i < 6; i++)
                {
                    packet[i] = 0xFF;
                }
                for (int i = 1; i <= 16; i++)
                {
                    for (int j = 0; j < 6; j++)
                    {
                        packet[i * 6 + j] = macBytes[j];
                    }
                }

                // 发送魔术包
                UdpClient client = new UdpClient();
                client.Connect(IPAddress.Broadcast, 4000);
                await client.SendAsync(packet, packet.Length);
                client.Close();

                // 在成功发送唤醒消息后，您可以在界面上显示相应的信息或日志
                Debug.WriteLine("已向 " + ipAddress + " 发送唤醒消息");
            }
            catch (Exception ex)
            {
                // 处理发送唤醒消息时的错误
                Debug.WriteLine("发送唤醒消息时出错：" + ex.Message);
            }
        }
        private static byte[] GetMacBytes(string macAddress)
        {
            byte[] bytes = new byte[6];
            string[] hex = macAddress.Split('-');
            if (hex.Length != 6)
            {
                throw new ArgumentException("Invalid MAC address.");
            }
            try
            {
                for (int i = 0; i < 6; i++)
                {
                    bytes[i] = Convert.ToByte(hex[i], 16);
                }
            }
            catch (FormatException)
            {
                throw new ArgumentException("Invalid MAC address.");
            }
            return bytes;
        }


        public void SendMessageToComputer(string ipAddress, string message)
        {
            try
            {
                // 创建目标终结点
                IPEndPoint targetEndPoint = new IPEndPoint(IPAddress.Parse(ipAddress), 1145);

                // 将消息转换为字节数组
                byte[] data = Encoding.UTF8.GetBytes(message);

                // 使用UDP客户端发送消息
                udpClient.Send(data, data.Length, targetEndPoint);

                // 在成功发送消息后，您可以在界面上显示相应的信息或日志
                Debug.WriteLine("已向 " + ipAddress + " 发送消息: " + message);
            }
            catch (Exception ex)
            {
                // 处理发送消息时的错误
                Debug.WriteLine("发送消息时出错：" + ex.Message);
            }
        }
    }
}
